package com.turing.usdtpay.http.callback;


import android.text.TextUtils;

import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.http.okhttp.model.Progress;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by $Jay
 * Date : 2019\1\15
 **/
public abstract class ApiCallback {
    private static String TAG = "ApiCallback";
    public abstract void onSuccess(int code, ResponseDate date);

    public abstract void onFailure(Throwable exception);

    public void onUIProgress(int progress){

    }
//    public void onSuccessFilter(int code,String json){
//        try {
//            JSONObject jsonObject = new JSONObject(json);
//            onSuccess(code,new ResponseDate(jsonObject.getInt("ret"),json,null));
//        } catch (JSONException e) {
//            e.printStackTrace();
//            onFailure(e);
//        }
//
//    }

    public void onSuccessFilter(int code,String json){
        LogUtils.d(TAG,"onSuccessFilter  code--->"+code+",json--->"+json);
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject object = jsonObject.optJSONObject("data");
            String data = "";
            if (object == null){
                data = "";
            }else {
                data = object.toString();
            }
            LogUtils.d("data : -->"+data);
            onSuccess(code,new ResponseDate(jsonObject.optInt("state"),data,jsonObject.optString("msg")));
        } catch (JSONException e) {
            e.printStackTrace();
            onFailure(e);
        }

    }

    public void onSuccessArrayFilter(int code,String json){
        LogUtils.d(TAG,"onSuccessFilter  code--->"+code+",json--->"+json);
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.optJSONArray("data");
            String data = "";
            if (jsonArray == null){
                data = "";
            }else {
                data = jsonArray.toString();
            }
            LogUtils.d("data : -->"+data);
            onSuccess(code,new ResponseDate(jsonObject.optInt("state"),data,jsonObject.optString("msg")));
        } catch (JSONException e) {
            e.printStackTrace();
            onFailure(e);
        }

    }

    public void onFailureFilter(Throwable exception){
        LogUtils.d(TAG,"onFailureFilter  exception--->"+exception.toString());
    }
}
